package cn.edu.zafu.bigdata.x.zzzznnn.f;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class AppendAndReadFile {
    public static void appendToFile(String fileName, String content) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true))) {
            writer.write("\n" + content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void readFile(String fileName) {
        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = reader.readLine())!= null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        java.util.Scanner scanner = new java.util.Scanner(System.in);
        String fileName = "files/202202010227.txt";
        System.out.println("请输入要追加到文件的文本内容：");
        String inputText = scanner.nextLine();
        appendToFile(fileName, inputText);
        System.out.println("文本已追加到文件，现在读取并显示文件内容：");
        readFile(fileName);
        scanner.close();
    }
}